package com.wudl.govern.controller;

import com.wudl.common.api.CommonResult;

import com.wudl.govern.domain.MetaLog;
import com.wudl.govern.service.MetaLogService;
import com.wudl.govern.vo.MetaLogVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * 元数据变更监控日志 Service
 *
 * @author wudl
 */
@Api
@RestController
@RequestMapping("/metaLog")
public class MetaLogController {

    @Autowired
    private MetaLogService metaLogService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public CommonResult<List<MetaLogVO>> page(@RequestBody MetaLogVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {

        List<MetaLogVO> MetaLogVoList = new ArrayList<>();
  // 拷贝结果
        List<MetaLog> MetaLogList = metaLogService.page(page, size);

        for (MetaLog metaLogTmp:MetaLogList )
        {
            MetaLogVO metaLogVO = new MetaLogVO();
            BeanUtil.copyProperties(metaLogTmp, MetaLogVO.class);
            MetaLogVoList.add(metaLogVO);
        }
        return CommonResult.success(MetaLogVoList);


    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<MetaLogVO> get(@PathVariable Integer id) {
        MetaLog entity = metaLogService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, MetaLogVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody MetaLogVO addVO) {
        metaLogService.save(BeanUtil.copyProperties(addVO, MetaLog.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody MetaLogVO editVO) {
        metaLogService.updateById(BeanUtil.copyProperties(editVO, MetaLog.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Integer id) {
        metaLogService.removeById(id);
        return CommonResult.success();
    }

}
